﻿using QFramework;
using System.Linq;

namespace ProjectIndieFarm
{
    public class SubItemCountCommand : AbstractCommand
    {
        private readonly string mItemName;
        private readonly int mSubCount;

        public SubItemCountCommand(string itemName, int subCount)
        {
            this.mItemName = itemName;
            this.mSubCount = subCount;
        }

        protected override void OnExecute()
        {
            var toolBarSystem = this.GetSystem<IToolbarSystem>();
            var slot = toolBarSystem.Slots
                .FirstOrDefault(item => item.ItemID.Value == mItemName);
            if (slot != null)
            {
                slot.Count.Value -= mSubCount;
                ToolbarSystem.OnItemCountChange
                    .Trigger(slot, slot.Count.Value);
            }
        }
    }
}
